package com.example.myproject.util;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.qiaofang.queue.bean.SendCommunicationData;
import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/21.
 */
public class TestSerialSendCommunicationData {
    public static void readobj()throws Exception
    {
        File file = new File("e:/rabbitmq.txt");
        List<String> str = readTxt(file);
        StringBuilder sb = new StringBuilder();
        for(String g : str){
            sb.append(g);
        }
        byte[] gg =  Base64.decodeBase64(sb.toString());

        //把最新的搞过来看这里是否会报错
        SendCommunicationData p = (SendCommunicationData)toObject(gg);
        System.out.println("data:"+ JSON.toJSON(p));
    }
    /**
     * 数组转对象
     * @param bytes
     * @return
     */
    public static  Object toObject (byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = ois.readObject();
            ois.close();
            bis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return obj;
    }



    public static List<String> readTxt(File file) {
        List<String> txtlist = new ArrayList<String>();
        try {
            String encoding = "UTF-8";
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTXT = null;
                while ((lineTXT = bufferedReader.readLine()) != null) {
                    txtlist.add(lineTXT.toString().trim());
                }
                read.close();
            } else {
                System.out.println("The File doesn't exist");
            }
        } catch (Exception e) {
            //log.error(e);
        }
        return txtlist;
    }



    public static void main(String[] args){
        try {
            readobj();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
